import { createWebHashHistory, createRouter } from 'vue-router'
const modules = import.meta.glob(['../view/*.vue'])
import { useRouter } from 'vue-router'

const routes = [
    {
        path: '/',
        redirect: "index"
    }
]

export const router = createRouter({
    history: createWebHashHistory(),
    routes: init_router(),
})

function init_router() {
    for (let [key, val] of Object.entries(modules)) {
        const name = key.split("/").pop()?.slice(0, -4) as string
        const route = {
            path: `/${name}`,
            name: name,
            component: val
        }
        routes.push(route)
    }
    return routes
}

router.beforeEach((to, from) => {
    const route = useRouter()
})

export default router